package com.jy.datapipeline.importserver.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jy.datapipeline.export.entity.DataStoreInfoModel;
import com.jy.datapipeline.export.service.DataStoreInfoService;
import com.jy.datapipeline.common.constants.ExpConstants;
import com.jy.datapipeline.importserver.common.dto.ImportStoreDto;
import com.jy.datapipeline.importserver.common.enums.impl.ReturnEnum;
import com.jy.datapipeline.common.exception.GlobalException;
import com.jy.datapipeline.importserver.service.ImportStoreService;
import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @类名 ImportStoreServiceImpl
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2023/4/19 15:52
 * @版本 1.0
 */
@Service
public class ImportStoreServiceImpl implements ImportStoreService {

    @Resource
    private DataStoreInfoService dataStoreInfoService;

    @Override
    public List<ImportStoreDto> queryAllNames() {
        List<Map<String,Object>> list = dataStoreInfoService.findDataStoreInfoByList(null,null,-1,-1);
        return JSONObject.parseArray(JSONObject.toJSONString(list), ImportStoreDto.class);
    }

    @Override
    public String queryStoreLocation(String storeName) throws GlobalException {
        List<Map<String, Object>> dataStoreInfoByList = dataStoreInfoService.findDataStoreInfoByList(storeName, null, 0, 1);
        String remotePath = null;
        if (dataStoreInfoByList != null && dataStoreInfoByList.size() > 0) {
            List<ImportStoreDto> importStoreDtos = JSONObject.parseArray(JSONObject.toJSONString(dataStoreInfoByList), ImportStoreDto.class);
            remotePath = importStoreDtos.get(0).getRemote_path();
            if (StringUtils.isNullOrEmpty(remotePath)) {
                throw new GlobalException(ReturnEnum.SERVER_ERROR.getCode(), ExpConstants.STORE_LOCATION_EXP);
            }
        }
        return remotePath;
    }

    @Override
    public DataStoreInfoModel queryStoreIdByStoreName(String storeName) {
        return dataStoreInfoService.findByStoreName(storeName);
    }
}
